package edu.guigu.todolist.login;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import edu.guigu.todolist.R;
import edu.guigu.todolist.base.BaseActivity;
import edu.guigu.todolist.login.presenter.LoginPresenterImpl;
import edu.guigu.todolist.login.view.LoginView;
import edu.guigu.todolist.todo.TodoActivity;

// MVP
public class LoginActivity extends BaseActivity implements LoginView {

    private LoginPresenterImpl presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_login);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        presenter = new LoginPresenterImpl(this);
    }

    @Override
    public void initView() {
        findViewById(R.id.bt_login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText et_username = findViewById(R.id.tiet_username);
                EditText et_password = findViewById(R.id.tiet_password);
                String userName = et_username.getText().toString();
                String password = et_password.getText().toString();
                presenter.loginVerify(userName, password);
            }
        });
    }

    @Override
    public void navigateToTodo() {
        Intent intent = new Intent(this, TodoActivity.class);
        startActivity(intent);
    }

    @Override
    public void showLoginFailed() {
        Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(this, TodoActivity.class);
        startActivity(intent);
    }

    @Override
    public void showLoading() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 当前页面销毁时，解除绑定,销毁Presenter中持有的View（防止内存泄漏）
        presenter.onDestroy();
    }
}